package com.prezent;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.facebook.model.GraphUser;

public class SelectionFragment extends Fragment {

	// do debugowania
	private static final String TAG = "SelectionFragment";

	private ListView listView;
	private ImageView imageView;
	private List<BaseListElement> listElements;
	private Button friendChoose;
	private EditText friendName;
	private EditText friendBirthday;
	private ImageView friendImage;

	private View view;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		View view = inflater.inflate(R.layout.selection, container, false);
		imageView = (ImageView) view.findViewById(R.id.friendImage);

		listElements = new ArrayList<BaseListElement>();
		listElements.add(new PeopleListElement(0));
		friendName = (EditText) view.findViewById(R.id.friendName);
		friendBirthday = (EditText) view.findViewById(R.id.friendBirthday);
		friendImage = (ImageView) view.findViewById(R.id.friendImage);
		friendChoose = (Button) view.findViewById(R.id.chooseFriend);
		friendChoose.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				friendName.setText("sfs fds");
			}
		});
		this.view = view;
		return view;
	}

	private void startPickerActivity(Uri data, int requestCode) {
		Intent intent = new Intent();
		intent.setData(data);
		intent.setClass(getActivity(), PickerActivity.class);
		startActivityForResult(intent, requestCode);
	}

	private class ActionListAdapter extends ArrayAdapter<BaseListElement> {
		private List<BaseListElement> listElements;

		public ActionListAdapter(Context context, int resourceId,
				List<BaseListElement> listElements) {
			super(context, resourceId, listElements);
			this.listElements = listElements;
			// Set up as an observer for list item changes to
			// refresh the view.
			for (int i = 0; i < listElements.size(); i++) {
				listElements.get(i).setAdapter(this);
			}
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = convertView;
			if (view == null) {
				LayoutInflater inflater = (LayoutInflater) getActivity()
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = inflater.inflate(R.layout.listitem, null);
			}

			BaseListElement listElement = listElements.get(position);
			if (listElement != null) {
				view.setOnClickListener(listElement.getOnClickListener());
				// ImageView icon = (ImageView) view.findViewById(R.id.icon);
				TextView text1 = (TextView) view.findViewById(R.id.text1);
				TextView text2 = (TextView) view.findViewById(R.id.text2);
				// if (icon != null) {
				// icon.setImageDrawable(listElement.getIcon());
				// }
				if (text1 != null) {
					text1.setText(listElement.getText1());
				}
				if (text2 != null) {
					text2.setText(listElement.getText2());
				}
			}
			return view;
		}
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		List<GraphUser> list = ((PresentApplication) getActivity()
				.getApplication()).getSelectedUsers();
		if (list.size() == 1) {
			GraphUser user = list.get(1);

			String name = user.getName();
			friendName.setText(name != null ? name : "wybierz znajomwgo");
			String birthady = user.getBirthday();
			friendBirthday.setText(birthady != null ? birthady : "wybierz");
		}
	}

	private class PeopleListElement extends BaseListElement {

		public PeopleListElement(int requestCode) {
			super(getActivity().getResources().getDrawable(
					R.drawable.ic_launcher), // TODO w tym miejscu ikona akcji
					getActivity().getResources().getString(
							R.string.action_people), getActivity()
							.getResources().getString(
									R.string.action_people_default),
					requestCode);
		}

		@Override
		protected View.OnClickListener getOnClickListener() {
			return new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					startPickerActivity(PickerActivity.FRIEND_PICKER,
							getRequestCode());
				}
			};
		}
	}
}
